package com.qb.main.ui.activity

import android.content.Intent
import android.graphics.Color
import android.os.Bundle
import android.view.View
import com.alibaba.android.arouter.facade.annotation.Route
import com.blankj.utilcode.util.SPStaticUtils
import com.blankj.utilcode.util.ToastUtils
import com.qb.baselibrary.basekotlin.EmptyViewModel
import com.qb.common.base.CommonBaseActivity
import com.qb.common.bean.EmptyData
import com.qb.common.bean.ImageBean
import com.qb.common.bean.MyImageBean
import com.qb.common.constant.Constants
import com.qb.common.router.RouterPath
import com.qb.common.utils.LitePalUtils
import com.qb.common.utils.ShareUtils
import com.qb.common.utils.ValidUtils
import com.qb.main.R
import com.qb.main.databinding.MainActivityRecordBinding
import com.qb.main.ui.adapter.ImageAdapter
import com.qb.main.ui.adapter.MyImageAdapter

@Route(path = RouterPath.Main.URL_Main_RecordActivity)
class RecordActivity:CommonBaseActivity<EmptyViewModel,MainActivityRecordBinding>(),View.OnClickListener {
    private var page = 1
    private var isLike = 1
    private val myImageAdapter by  lazy {
        MyImageAdapter()
    }
    override fun initView(savedInstanceState: Bundle?) {

        mBinding.baseTitle.setTitle("记录")
        mBinding.baseTitle.setOnBack()
        mBinding.tvLike.setOnClickListener(this)
        mBinding.tvBad.setOnClickListener(this)
        with(mBinding.recycle){
            adapter = myImageAdapter
        }
        myImageAdapter.apply {
            addChildClickViewIds(R.id.iv_gif)
            addChildClickViewIds(R.id.iv_png)
            addChildClickViewIds(R.id.tv_good)
            addChildClickViewIds(R.id.tv_bad)
            addChildClickViewIds(R.id.tv_share)
            setOnItemChildClickListener { adapter, view, position ->
                var imageBean = adapter.getItem(position) as MyImageBean
                when(view.id){
                    R.id.iv_gif->{

                    }
                    R.id.iv_png->{

                    }
                    R.id.tv_good->{

                    }
                    R.id.tv_bad->{

                    }
                    R.id.tv_share->{
//                        val wechatIntent = Intent(Intent.ACTION_SEND)
//                        wechatIntent.setPackage("com.tencent.mm")
//                        wechatIntent.type = "text/plain"
//                        wechatIntent.putExtra(Intent.EXTRA_TEXT, imageBean.img)
//                        startActivity(wechatIntent)
                        ShareUtils.share(mContext,imageBean.img)
                    }
                }
                notifyItemChanged(position)
            }
        }
        mBinding.smartRefresh.setOnRefreshListener {
            refData()
        }
        mBinding.smartRefresh.setOnLoadMoreListener {
            getData()
        }
        mBinding.tvLike.setTextColor(Color.parseColor("#BB996C"))
        mBinding.tvBad.setTextColor(Color.parseColor("#333333"))
        isLike = 1
        refData()

    }
    fun getData(){
        var allImageBeans = LitePalUtils.queryLikeOrBad(page,isLike)
        if (page==1){
            myImageAdapter.setNewInstance(allImageBeans)
            mBinding.smartRefresh.finishRefresh()
        }else{
            myImageAdapter.addData(allImageBeans)
            mBinding.smartRefresh.finishLoadMore()
        }
        if (ValidUtils.isValid(allImageBeans)){
            page++
        }
    }
    override fun initData() {
    }
    fun refData(){
        page = 1
        myImageAdapter.setNewInstance(null)
        getData()
    }
    override fun onClick(v: View?) {
        v?.let {
            when(v.id){
                mBinding.tvLike.id->{
                    mBinding.tvLike.setTextColor(Color.parseColor("#BB996C"))
                    mBinding.tvBad.setTextColor(Color.parseColor("#333333"))
                    isLike = 1
                    refData()
                }
                mBinding.tvBad.id->{
                    mBinding.tvLike.setTextColor(Color.parseColor("#333333"))
                    mBinding.tvBad.setTextColor(Color.parseColor("#BB996C"))
                    isLike = 0
                    refData()
                }
            }
        }
    }
}